package com.zzyl.service.impl;

import com.zzyl.entity.AccraditationRecord;
import com.zzyl.mapper.AccraditationRecordMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.AccreditationRecordService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RecordVo;
import com.zzyl.vo.UserRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class AccreditationRecordServiceImpl implements AccreditationRecordService {

    @Autowired
    private AccraditationRecordMapper accraditationRecordMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(RecordVo recoreVo) {
        AccraditationRecord accraditationRecord = new AccraditationRecord();

        accraditationRecord.setHandleType(recoreVo.getHandleType());
        accraditationRecord.setAuditStatus(recoreVo.getStatus());
        accraditationRecord.setBussniessId(recoreVo.getId());
        accraditationRecord.setCurrentStep(recoreVo.getStep());
        accraditationRecord.setCreateTime(LocalDateTime.now());
        accraditationRecord.setOpinion(recoreVo.getOption());
        accraditationRecord.setType(recoreVo.getType());
        accraditationRecord.setApproverId(recoreVo.getUserId());
        accraditationRecord.setApproverName(recoreVo.getRealName());

        //当前申请步骤
        //入住申请
        Long increment = redisTemplate.boundValueOps("RD" + recoreVo.getId().toString()).increment();
        accraditationRecord.setStepNo(increment);

        //下一个审核人
        if(ObjectUtil.isNotEmpty(recoreVo.getNextAssignee())){
            accraditationRecord.setNextStep(recoreVo.getNextStep());

            List<UserRoleVo> userRoleVos = userRoleMapper.selectByUserId(recoreVo.getNextAssignee());
            UserRoleVo userRoleVo = userRoleVos.get(0);
            accraditationRecord.setNextApprover(userRoleVo.getUserName());
            accraditationRecord.setNextApproverId(userRoleVo.getId());
            accraditationRecord.setNextApproverRole(userRoleVo.getRoleName());

        }

        accraditationRecordMapper.insert(accraditationRecord);

    }
}
